//
//  ResultDialog.m
//  Cai8
//
//  Created by zyn on 2017/6/19.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "ResultDialog.h"
#import "Config.h"

@implementation ResultDialog

- (instancetype)initWithNumber:(NSInteger) number type:(NSInteger )type vc:(UIViewController *)vc{
    self = [super init];
    if (self) {
        superVC = vc;
        resultNumber = @"";
        
        self.frame = CGRectMake(0, 0,ScreenWidth(), ScreenHeight());
        self.backgroundColor = [UIColor clearColor];
        
        // 背景
        ivBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth(), ScreenHeight())];
        ivBg.backgroundColor = [UIColor blackColor];
        ivBg.alpha = 0;
        [self addSubview:ivBg];
        
        // 容器
        container = [[UIView alloc]initWithFrame:CGRectMake(30, 100, ScreenWidth() - 60, ScreenHeight() - 200)];
        container.backgroundColor = [UIColor whiteColor];
        container.layer.borderColor = [[UIColor grayColor]CGColor];
        container.layer.borderWidth = .5;
        container.layer.cornerRadius = 6;
        container.layer.masksToBounds = YES;
        [self addSubview:container];
        
        // 底部取消按钮
        UIButton *btnCancel = [[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight() - 200 - 40, (ScreenWidth() - 60) / 2 - 0.5, 40)];
        [btnCancel setBackgroundColor:[UIColor purpleColor]];
        [btnCancel setTitle:NSLocalizedString(@"取消", @"取消") forState:UIControlStateNormal];
        [container addSubview:btnCancel];
        [btnCancel addTarget:self action:@selector(onClickCancel:) forControlEvents:UIControlEventTouchUpInside];
        
        // 底部取消按钮
        UIButton *btnShare = [[UIButton alloc]initWithFrame:CGRectMake((ScreenWidth() - 60) / 2 + 0.5, ScreenHeight() - 200 - 40, (ScreenWidth() - 60) / 2 - 0.5, 40)];
        [btnShare setBackgroundColor:[UIColor purpleColor]];
        [btnShare setTitle:NSLocalizedString(@"分享", @"分享") forState:UIControlStateNormal];
        [container addSubview:btnShare];
        [btnShare addTarget:self action:@selector(onClickShare:) forControlEvents:UIControlEventTouchUpInside];
        
        // 推荐号码文案
//        labelTitle = [[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth() - 60 - 100)/2, 30, 100, 20)];
        labelTitle = [[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth() - 60 - 100)/2, 30 + 100, 100, 20)];
        labelTitle.alpha = 0;
        [labelTitle setFont:[UIFont systemFontOfSize:18]];
        [labelTitle setTextColor:[UIColor blackColor]];
        labelTitle.textAlignment = NSTextAlignmentCenter;
        [container addSubview:labelTitle];
        
        switch (type) {
            case 11:
                labelTitle.text = NSLocalizedString(@"双色球", @"双色球");
                break;
                
            case 14:
                labelTitle.text = NSLocalizedString(@"大乐透", @"大乐透");
                break;
                
            case 16:
                labelTitle.text = NSLocalizedString(@"排列三", @"排列三");
                break;
                
            case 17:
                labelTitle.text = NSLocalizedString(@"排列五", @"排列五");
                break;
                
            case 75:
                labelTitle.text = NSLocalizedString(@"江苏快三", @"江苏快三");
                break;
                
            case 80:
                labelTitle.text = NSLocalizedString(@"湖北快三", @"湖北快三");
                break;
                
            default:
                break;
        }

        
        [self addWidget:type];
        
    }
    return self;
    
}



-(void)onClickCancel:(UIButton *)btn{
    [UIView animateWithDuration:.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)onClickShare:(UIButton *)btn{
    //分享的标题
    NSString *textToShare = [resultNumber stringByAppendingString:@"  快去试试手气吧～"];
    //分享的图片
    //在这里呢 如果想分享图片 就把图片添加进去  文字什么的通上
    NSArray *activityItems = @[textToShare];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    //不出现在活动项目
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    [superVC presentViewController:activityVC animated:YES completion:nil];
    // 分享之后的回调
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            NSLog(@"completed");
            //分享 成功
        } else  {
            NSLog(@"cancled");
            //分享 取消
        }
    };
}

-(void)show{
    [UIView animateWithDuration:.5 animations:^{
        ivBg.alpha = .5;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.5 animations:^{
            [labelTitle setFrame: CGRectMake((ScreenWidth() - 60 - 100)/2, 30, 100, 20)];
            labelTitle.alpha = 1;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:.5 animations:^{
                [contentView setFrame:CGRectMake(0, 80, ScreenWidth(), (ScreenWidth() - 60 - 8 * 12) / 7)];
                contentView.alpha = 1;
            }];
        }];
        
       
    }];
    
    
//    [self translationAnimation];
    
    
}

-(void)translationAnimation{
    
    CAKeyframeAnimation *orbit = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    //2.设置关键帧,这里有四个关键帧
    CATransform3D scale1 = CATransform3DMakeScale(0.0, 0.0, 1);
    CATransform3D scale2 = CATransform3DMakeScale(1.1, 1.1, 1);
    CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
    CATransform3D scale4 = CATransform3DMakeScale(1.0, 1.0, 1);
    
    NSValue *key1=[NSValue valueWithCATransform3D:scale1];//对于关键帧动画初始值不能省略
    NSValue *key2=[NSValue valueWithCATransform3D:scale2];
    NSValue *key3=[NSValue valueWithCATransform3D:scale3];
    NSValue *key4=[NSValue valueWithCATransform3D:scale4];
    NSArray *values=@[key1,key2,key3,key4];
    
    orbit.duration = 0.4;
    orbit.values = values;
    orbit.keyTimes = @[@0.0f,@0.6f,@0.8f,@1.0f];
    
    orbit.beginTime = CACurrentMediaTime();
    [container.layer addAnimation:orbit forKey:@"orbit"];
}


// 设置号码
/// 双色球11 大乐透14 排列三16 排列五17 江苏快三75 湖北快三80 安徽快三 76
-(void)addWidget:(NSInteger)type{
//    contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 80, ScreenWidth(), (ScreenWidth() - 60 - 8 * 12) / 7)];
    contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 80 + 100, ScreenWidth(), (ScreenWidth() - 60 - 8 * 10) / 7)];

    // 双色球 and 大乐透
    if (type == 11 || type == 14) {
        type == 11 ? resultNumber  = @"双色球 今日幸运号码：":(resultNumber =@"大乐透 今日幸运号码：");
        int width = (ScreenWidth() - 60 - 8 * 10) / 7;
        for (int i =0; i < 7; i++) {
            UIButton *ball = [[UIButton alloc]initWithFrame:CGRectMake(10 * (i + 1) + i * width, 0, width, width)];
            i < 6 ? (ball.backgroundColor = [UIColor redColor]) : (ball.backgroundColor = [UIColor blueColor] );
            int number = 0;
            if (type == 11) {
                number = i < 6 ? arc4random() % 33 + 1 : arc4random() % 16 + 1;
            }else{
                number = i < 6 ? arc4random() % 35 + 1 : arc4random() % 12 + 1;
            }
            
            number < 10 ? [ball setTitle:[NSString stringWithFormat:@"0%d",number] forState:UIControlStateNormal] : [ball setTitle:[NSString stringWithFormat:@"%d",number] forState:UIControlStateNormal];
            [ball setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            ball.layer.cornerRadius = width / 2;
            ball.layer.masksToBounds = YES;
            [ball.titleLabel setFont:[UIFont systemFontOfSize:15]];
            
            if (type == 14 && i == 5)
                ball.backgroundColor = [UIColor blueColor] ;
            
            [contentView addSubview:ball];
            resultNumber = [resultNumber stringByAppendingString:[NSString stringWithFormat:@"%d ",number]];

        }
    }
    
        
        // 排列三
     if (type == 16) {
         resultNumber  = @"排列三 今日幸运号码：";
            int width = (ScreenWidth() - 60 - 8 * 10) / 7;
            for (int i =0; i < 3; i++) {
                UIButton *ball = [[UIButton alloc]initWithFrame:CGRectMake(( (ScreenWidth() - 60) - (20 + width * 3)) / 2 + i * 10 + width * i, 0, width, width)];
                ball.backgroundColor = [UIColor redColor];
                int number = arc4random() % 9 + 1;
                [ball setTitle:[NSString stringWithFormat:@"%d",number] forState:UIControlStateNormal] ;
                [ball setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                ball.layer.cornerRadius = width / 2;
                ball.layer.masksToBounds = YES;
                [ball.titleLabel setFont:[UIFont systemFontOfSize:15]];
                
                [contentView addSubview:ball];
                
                resultNumber = [resultNumber stringByAppendingString:[NSString stringWithFormat:@"%d ",number]];


            }

        }
        
        // 排列五
        if (type == 17) {
            resultNumber  = @"排列五 今日幸运号码：";

            int width = (ScreenWidth() - 60 - 8 * 10) / 7;
            for (int i =0; i < 5; i++) {
                UIButton *ball = [[UIButton alloc]initWithFrame:CGRectMake(( (ScreenWidth() - 60) - (40 + width * 5)) / 2 + i * 10 + width * i, 0, width, width)];
                ball.backgroundColor = [UIColor redColor];
                int number = arc4random() % 9 + 1;
                [ball setTitle:[NSString stringWithFormat:@"%d",number] forState:UIControlStateNormal] ;
                [ball setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                ball.layer.cornerRadius = width / 2;
                ball.layer.masksToBounds = YES;
                [ball.titleLabel setFont:[UIFont systemFontOfSize:15]];
                
                [contentView addSubview:ball];
                resultNumber = [resultNumber stringByAppendingString:[NSString stringWithFormat:@"%d ",number]];


            }
            
        }
    
    // 快三
    if(type == 75 || type == 80){
        type == 75 ? resultNumber  = @"江苏快三 今日幸运号码：": (resultNumber =@"湖北快三 今日幸运号码：");

        int width = (ScreenWidth() - 60 - 4 * 30) / 3;
        for (int i =0; i < 3; i++) {
            UIImageView *ball = [[UIImageView alloc]initWithFrame:CGRectMake(30 * (i + 1) + i * width, 0, width, width)];
            int number = arc4random() % 6 + 1;
            [ball setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",number]]];
            
            [contentView addSubview:ball];
            resultNumber = [resultNumber stringByAppendingString:[NSString stringWithFormat:@"%d ",number]];

        }

    }
    
    
        [container addSubview:contentView];
        contentView.alpha = 0;

    

    
    
    
}





@end
